package com.qianxx.component.activity.text.wheel_view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.qianxx.component.R;
import com.qianxx.viewlibrary.text.wheelview.ArrayWheelAdapter;
import com.qianxx.viewlibrary.text.wheelview.WheelView;

import butterknife.ButterKnife;

public class WheelViewActivity extends AppCompatActivity {
    private Dialog dialog;
    private String[] data={"新闻","科技","财经","军事","国际","专题","汽车","娱乐","体育"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wheel_view);
        ButterKnife.bind(this);

        if(dialog == null)
            dialog = selectDialog(this, new OnClickListener() {
                @Override
                public void onCancel() {
                    if(dialog != null)
                        dialog.dismiss();
                }

                @Override
                public void onComfig(int position) {
                    if(dialog != null)
                        dialog.dismiss();
                    Toast.makeText(getApplicationContext(),data[position],Toast.LENGTH_SHORT).show();
                }
            },data);
        dialog.show();
    }

    /**
     * 滚筒式，服务选择，对话框
     *
     * @param context
     * @param listener
     * @param data
     * @return
     */
    public static Dialog selectDialog(Context context, final OnClickListener listener, String[] data) {
        Dialog dialog = new Dialog(context, R.style.select_image_dialog);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_select, null);
        dialog.setContentView(view);

        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);

        TextView cancel = (TextView) view.findViewById(R.id.cancel);
        TextView comfig = (TextView) view.findViewById(R.id.comfig);
        final WheelView wv = (WheelView) view.findViewById(R.id.wv);
        //设置WheelView在屏幕中最大显示元素个数
        wv.setVisibleItems(4);
        //设置WheelView 元素是否循环滚动
        wv.setCyclic(true);
        //设置WheelView 的元素适配器
        wv.setAdapter(new ArrayWheelAdapter<String>(data));
        wv.setCurrentItem(2);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onCancel();
            }
        });
        comfig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onComfig(wv.getCurrentItem());
            }
        });

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(view);
        return dialog;
    }

    public interface OnClickListener {
        void onCancel();

        void onComfig(int position);
    }
}
